<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="java.util.List"%>
<%@page import="com.googlecode.contests_repo.core.model.ResourcesUtils"%>
<%@page import="com.googlecode.contests_repo.core.model.Series"%>
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page
	import="java.util.ArrayList,com.googlecode.contests_repo.core.model.Resource"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Series list</title>
</head>
<body>
	<h1>List of all available series</h1>
	<br />
	<%
		String userName = (String) session.getAttribute("username");
		String path = request.getParameter("path"); // relative path 
		boolean canEdit = "1".equals(request.getParameter("edit"));
		String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
		String resourcePath = repoRoot + File.separator + path;
		boolean hasUserEditRights = UserManager.hasAccessRight(userName,
				resourcePath, AccessRight.VIEW_FULL);
	%>
	<table border=1>
		<tr>
			<th>Id</th>
			<th>Title</th>
			<th>About</th>
			<th>View</th>
			<%
				if(canEdit && hasUserEditRights){
			%>
				<th>Edit</th>
				<th>Delete</th>
			<%
				}
			%>
		</tr>
		<%
		    String loggedUser = (String)session.getAttribute("username");
				    String seriesPath = ConfigurationManager.getInstance().getSeriesPath();
				    String seriesRelativePath = ConfigurationManager.getInstance().getSeriesRelativePath();
				    List<Series> seriesList = ResourcesUtils.fetchResources(new File(seriesPath));
				    if (seriesList != null)
				    {
						for (Series series : seriesList)
						{
							String seriesDir = seriesPath + File.separator + series.getId();
						    if (hasUserEditRights || UserManager.hasAccessRight(loggedUser, seriesDir, AccessRight.LIST))
						    {
		%>
		<tr>
			<td><%=series.getId()%></td>
			<td><%=series.getTitle()%></td>
			<td><%=series.getAbout()%></td>
			<td><a href="resource.jsp?path=<%=seriesRelativePath + File.separator + series.getId()%>">details</a></td>
		<%
		    		}
						    if(canEdit && UserManager.hasAccessRight(loggedUser, seriesDir, AccessRight.VIEW_FULL)){
		%>
			<td><a href="resource.jsp?path=<%=seriesRelativePath + File.separator + series.getId()%>&edit=1">edit</a></td>
			<td><a href="delete">delete</a></td>
		<%
						    	
						    }
		%>
		
			</tr>
		<%
		    	}
		    }
		%>
	</table>
	<br />
	<br />
</body>
</html>